package ude.univ.esiag.gon.uchiha.publisher;

import java.util.Vector;

import ude.univ.esiag.gon.uchiha.operation.Operation;
import ude.univ.esiag.gon.uchiha.subscriber.Semaphore;
import ude.univ.esiag.gon.uchiha.subscriber.Subscriber;

public class Publisher {
	private Vector<Subscriber> subscribers;
	private Semaphore semaphore; 
	public Publisher() {
		subscribers = new Vector<Subscriber>();
		semaphore = new Semaphore();
	}
	synchronized public void publish(Operation operation){
		semaphore.stop();
		for(int i = 0;i<subscribers.size();i++){
			try{
				subscribers.get(i).publish(operation);
			}catch(Exception e){
				System.out.println("ERROR : " + e.getMessage());
			}
		}
		semaphore.signal();
	}
	public void addSubscriber(Subscriber subscriber){
		semaphore.stop();
		this.subscribers.add(subscriber);
		semaphore.signal();
	}
}
